package com.ruoyi.web.controller.drinkwater;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.drinkwater.service.IBusWaterDrinkSourceManualService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 手动监测站
 */
@RestController
@RequestMapping("/drink/manual")
public class BusWaterDrinkSourceManualController extends BaseController {


    @Resource
    private IBusWaterDrinkSourceManualService busWaterDrinkSourceManualService;




    /**
     * @title getManualWaterLastTime
     * @description  获取手工监测断面评价最新的时间，支持传多个断面id
     * @param request
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @RequestMapping("/getManualWaterLastTime")
    public AjaxResult getManualWaterLastTime(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String,Object> params = new HashMap<>();
        for (String s : parameterMap.keySet()) {
            params.put(s,String.join(",",parameterMap.get(s)));
        }
        return AjaxResult.success(busWaterDrinkSourceManualService.getManualWaterLastTime(params));
    }

    /**
     * @title getManualWaterDataList
     * @description  获取手工监测断面监测数据
     * @param request
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/getManualWaterDataList")
    public Map getManualWaterDataList(Integer pageNum, Integer pageSize, String segmentId, String beginTime, String endTime, String prop, String itemCode,  String compliance){
        Map map = new HashMap(); //存放传入参数

        if (prop != null && !"".equals(prop)) {
            if (prop.equals("CBF8")) {
                map.put("prop", "CBF8");
            } else {
                map.put("prop", prop);
            }
        } else {
            map.put("prop", "CBF8");
        }
        List<String> segmentids = null;
        if (segmentId != null && !"".equals(segmentId)) {
            String[] arr = segmentId.split(",");
            segmentids = Arrays.asList(arr);
            map.put("segmentids", segmentids);

        }
        List<String> itemCodes = null;
        if (itemCode != null && !"".equals(itemCode)) {
            String[] arr = itemCode.split(",");
            itemCodes = Arrays.asList(arr);
            map.put("itemCodes", itemCodes);
        }
        if (beginTime != null && !"".equals(beginTime)) {
            map.put("beginTime", beginTime + "-01");
        } else {
            map.put("beginTime", beginTime);
        }
        if (endTime != null && !"".equals(endTime)) {
            String yearMonthLastDay = DateUtils.getYearMonthLastDay(endTime);
            map.put("endTime", yearMonthLastDay);
        } else {
            map.put("endTime", endTime);
        }

        if ("0".equals(compliance)) {
            map.put("compliance", "");//是否达标
        } else if ("1".equals(compliance)) {
            map.put("compliance", compliance);//是否达标
        } else {//查询全部
            map.put("compliance", "");//是否达标
        }

        //查询处理后的结果数据值
        List<Map<String, Object>> manualWqDataBasicsmonitordata = busWaterDrinkSourceManualService.isManualWqDataBasicsmonitordata(map);
        //没有办法调用工具和sql进行分页调用Java 对应方法进行分页查询
        List<Map> collect = manualWqDataBasicsmonitordata.stream().skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());
        //获取总条数
        int size = manualWqDataBasicsmonitordata.size();
        //返回数据结果集
        Map maplist = new HashMap();
        maplist.put("data", collect);
        maplist.put("code", 200);
        maplist.put("msg", "查询成功");
        maplist.put("total", size);
        return maplist;

    }



}
